'''
路径参数
    注意：路径是从上往下匹配的，
    例如 /users/me 在前，/users/{user_id} 在后，先匹配 /users/me
    例如 /users/{user_id} 在前，/users/me 在后，先匹配 /users/{user_id}
'''
from enum import Enum
from typing import Union

from fastapi import APIRouter, Path, Query

router = APIRouter()


@router.get("/path/hello", tags=["路径参数"])
async def say_hello_1():
    return {"message": f"Hello"}


@router.get("/path/hello/{name}", tags=["路径参数"])
async def say_hello_2(name: str):
    return {"message": f"Hello {name}"}


@router.get("/path/hello/{name}/{age}", tags=["路径参数"])
async def say_hello_3(name: str, age: int):
    return {"message": f"Hello {name} age is {age}"}


# 路径参数设置预设值
class ModelName(str, Enum):
    alexnet = "alexnet"
    resnet = "resnet"
    lenet = "lenet"


@router.get("/path/models/{model_name}", tags=["路径参数"])
async def get_model(model_name: ModelName):
    if model_name == ModelName.alexnet:
        return {"model_name": model_name, "message": "Deep Learning FTW!"}

    if model_name.value == "lenet":
        return {"model_name": model_name, "message": "LeCNN all the images"}

    return {"model_name": model_name, "message": "Have some residuals"}


@router.get("/path/items/{item_id}", tags=["路径参数"])
async def read_items(item_id: int = Path(title="The ID of the item to get"),
                     q: Union[str, None] = Query(default=None, alias="item-query"), ):
    results = {"item_id": item_id}
    if q:
        results.update({"q": q})
    return results
